package org.xems.utils
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.describeType;

	/**
	 * 
	 * @author qixiaowei
	 * 
	 */
	public class ObjectUtil
	{
		/**
		 * 获取实例对象所有属性名称
		 * # 配合for...in...可以实现属性遍历操作
		 * @param instance
		 * @return 
		 */
		public static function getPropertyNamesByInstance(instance:*):Array
		{
			var pNameList:XMLList = describeType(instance).accessor.@name;
			var pNames:Array = new Array();
			for each(var node:XML in pNameList){
				pNames.push(node.toString());
			}
			return pNames;
		}
		
		/**
		 * 深度克隆URLLoader加载的显示对象
		 * @param bytes
		 * @param loaderContext
		 * @return 
		 */
		public static function cloneLoader(bytes:ByteArray, loaderContext:LoaderContext):Loader
		{
			var loader:Loader = new Loader();
			loader.loadBytes(bytes, loaderContext);
			return loader;
		}
		
		/**
		 * 克隆对象
		 * @param target
		 * @return 
		 * 
		 */
		public static function cloneObject(target:*):*
		{
			var raw:ByteArray = new ByteArray();
			raw.writeObject(target);
			return raw.readObject();
		}
	}
}